$(function() {

    //手机登录
    $(document).on('click','.mobileSave' ,function(){
        var mobilePhone = $('#mobilePhone').val();
        if (mobilePhone == "") {
            Prompt.show("手机号不能为空!",'提示',function(){});
            return false;
        }
        if (!checkMobile(mobilePhone)) {
            Prompt.show("手机号格式错误!",'提示',function(){});
            return false;
        }

        var yanzheng = $('#yanzheng').val();
        if (yanzheng == "") {
            Prompt.show("验证码不能为空!",'提示',function(){});
            return false;
        }

        var checkCode = $('#mcheckCode').html();

        if (yanzheng.toUpperCase() != checkCode) {
            Prompt.show("验证码不正确!",'提示',function(){});
            return false;
        }

        app.post(app.url.api_base+"login/resetPasswordByPhone.do",{phone:mobilePhone},
            function(req) { 
                if(req.code==0){
                    Prompt.show(req.message);
                    app.go('login.html');
                }else{
                    Prompt.show(req.message);
                }
            },false);
        changeCode(0);
    });

    function validEmail(email){
            var reg = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; 
            var objExp=new RegExp(reg);
            if(objExp.test(email)==true){ 
                return true; 
            }else{ 
                return false; 
            }
        }

        function checkMobile(phone){
            var reg = /^1\d{10}$/;
            var objExp=new RegExp(reg); 
            if(objExp.test(phone)==true){ 
                return true; 
            }else{ 
                return false; 
            }
        }

    //手机登录
    $(document).on('click','.emailSave' ,function(){
        var email = $('#EuserName').val();
        if (email == "") {
            Prompt.show("邮箱不能为空!",'提示',function(){});
            return false;
        }
        if (!validEmail(email)) {
            Prompt.show("邮箱格式错误!",'提示',function(){});
            return false;
        }

        var yanzheng = $('#Eyanzheng').val();
        if (yanzheng == "") {
            Prompt.show("验证码不能为空!",'提示',function(){});
            return false;
        }

        var checkCode = $('#echeckCode').html();

        if (yanzheng.toUpperCase() != checkCode) {
            Prompt.show("验证码不正确!",'提示',function(){});
            return false;
        }

        app.post(app.url.api_base+"login/resetPasswordByEmail.do",{email:email},
            function(req) { 
                if(req.code==0){
                    app.go('login.html');
                    Prompt.show(req.message,'提示',function(){});
                }else{
                    Prompt.show(req.message);
                }
            },false);
        changeCode(1);
    });

    // tab切换
    $(document).on('click','.Comtab li',function(){
        $(this).addClass('active').siblings().removeClass('active')
        $(".TabBox>div").eq($(this).index()).show().siblings().hide();
        changeCode($(this).index());
    })

    //btn
    function myBrowser() {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isOpera = userAgent.indexOf("Opera") > -1;
        if (isOpera) {
            return "Opera"
        }; //判断是否Opera浏览器
        if (userAgent.indexOf("Firefox") > -1) {
            return "FF";
        } //判断是否Firefox浏览器
        if (userAgent.indexOf("Chrome") > -1) {
            return "Chrome";
        }
        if (userAgent.indexOf("Safari") > -1) {
            return "Safari";
        } //判断是否Safari浏览器
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
            return "IE";
        }; //判断是否IE浏览器
    }

    //以下是调用上面的函数
    var mb = myBrowser();
    if ("Chrome" == mb) {
        new TouchRipple('.butsubmit', {
            color: 'rgba(255,255,255,.2)',
            time: 800,
            size: null
        });
    }

    //点击生产新的验证码
    $(document).on('click', '.YanzmLogin', function() {
        changeCode(0);
    })

    $(document).on('click', '.YanzeLogin', function() {
        changeCode(1);
    })

    //生成验证码
    changeCode(0);
    var code;

    function changeCode(type) {
        code = new Array();
        var codeLength = 4; //验证码的长度
        var checkCode = null;
        if (type == 0) {
            checkCode = document.getElementById("mcheckCode");
        } else {
            checkCode = document.getElementById("echeckCode");
        }
        
        checkCode.value = "";
        var selectChar = new Array(2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        for (var i = 0; i < codeLength; i++) {
            var charIndex = Math.floor(Math.random() * 32);
            code += selectChar[charIndex];
        }
        if (code.length != codeLength) {
            createCode();
        }
        checkCode.innerHTML = code;
    }

})

